package com.mooc.ppjoke.ui.my_view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.mooc.lib_common.PixUtils;
import com.mooc.ppjoke.R;
import com.mooc.ppjoke.exoplayer.IPlayTarget;
import com.mooc.ppjoke.exoplayer.PageListPlay;
import com.mooc.ppjoke.exoplayer.PagedListPlayerManager;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.ui.PlayerControlView;
import com.google.android.exoplayer2.ui.PlayerView;

import org.jetbrains.annotations.NotNull;

/**
 * @ClassName: NetPlayerView$
 * @Description: java类作用描述
 * @Author: 魏靳元
 * @CreateDate: 2022/8/17 11:09
 * @UpdateRemark: 更新内容
 * @Version: 1.0
 */
public class NetPlayerView extends FrameLayout implements IPlayTarget, PlayerControlView.VisibilityListener, Player.EventListener {

    protected View bufferView;
    protected NetImageView cover, blur;
    protected ImageView playbtn;
    protected String category;
    protected String videoUrl;
    protected boolean isPlaying;
    protected int mWidthPx;
    protected int mHeightPx;

    public NetPlayerView(@NonNull Context context) {
        this(context, null);
    }

    public NetPlayerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NetPlayerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);

    }

    public NetPlayerView(@NonNull @NotNull Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        LayoutInflater.from(context).inflate(R.layout.layout_play_view, this, true);
        initView();
    }

    public void bindData(String category, int widthPx, int heightPx, String coverUrl, String videoUrl){
        this.category = category;
        this.videoUrl = videoUrl;
        mWidthPx = widthPx;
        mHeightPx = heightPx;
        NetImageView.setImgUrl(cover,coverUrl,false);
        if(widthPx<heightPx){
            NetImageView.setBlurImageUrl( blur,coverUrl, 10);
            blur.setVisibility(VISIBLE);
        }else {
            blur.setVisibility(INVISIBLE);
        }
        setSize(widthPx,heightPx);
    }

    protected void setSize(int widthPx, int heightPx) {
        int maxWidth= PixUtils.getScreenWidth();
        int maxHeight=maxWidth;
        int layoutWidth=maxWidth;
        int layoutHeight=0;
        int coverWidth;
        int coverHeight;
        if(widthPx>=heightPx){
            coverWidth=maxWidth;
            layoutHeight = coverHeight = (int) (heightPx * ((widthPx * 1.0f) / maxWidth));
        }else {
            layoutHeight=coverHeight=maxHeight;
            coverWidth= (int) (widthPx*(heightPx*1.0f/maxHeight));
        }
        ViewGroup.LayoutParams layoutParams = getLayoutParams();
        layoutParams.width=layoutWidth;
        layoutParams.height=layoutHeight;
        setLayoutParams(layoutParams);
        ViewGroup.LayoutParams params = blur.getLayoutParams();
        params.width=layoutWidth;
        params.height=layoutHeight;
        blur.setLayoutParams(params);

        FrameLayout.LayoutParams coverLayoutParams = (LayoutParams) cover.getLayoutParams();
        coverLayoutParams.height=coverHeight;
        coverLayoutParams.width=coverWidth;
        coverLayoutParams.gravity= Gravity.CENTER;
        cover.setLayoutParams(coverLayoutParams);

        FrameLayout.LayoutParams playbtnLayoutParams = (LayoutParams) playbtn.getLayoutParams();
        playbtnLayoutParams.gravity= Gravity.CENTER;
        playbtn.setLayoutParams(playbtnLayoutParams);

    }

    private void initView() {
        cover=findViewById(R.id.cover);
        bufferView = findViewById(R.id.buffer_view);
        blur =  findViewById(R.id.blur_background);
        playbtn =  findViewById(R.id.play_btn);

        playbtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isPlaying){
                    inActive();
                }else {
                    onActive();
                }
            }
        });
        this.setTransitionName("listPlayerView");
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        PageListPlay pageListPlay = PagedListPlayerManager.get(category);
        PlayerControlView controllerView = pageListPlay.controllerView;
        controllerView.show();

        return true;
    }

    @Override
    public ViewGroup getOwner() {
        return this;
    }

    @Override
    public void onActive() {
        PageListPlay pageListPlay = PagedListPlayerManager.get(category);
        PlayerView playerView=pageListPlay.playerView;
        PlayerControlView controllerView = pageListPlay.controllerView;
        SimpleExoPlayer exoPlayer = pageListPlay.exoPlayer;
        ViewParent parent = playerView.getParent();
        if(parent!=this){
            if(parent!=null){
                ((ViewGroup)parent).removeView(playerView);
            }
            ViewGroup.LayoutParams coverLayoutParams = cover.getLayoutParams();
            this.addView(playerView,1,coverLayoutParams);
        }

        ViewParent controllerViewParent = controllerView.getParent();
        if(controllerViewParent!=this){
            if(controllerViewParent!=null){
                ((ViewGroup)controllerViewParent).removeView(controllerView);
            }
            LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            layoutParams.gravity=Gravity.BOTTOM;
            this.addView(controllerView,layoutParams);
            controllerView.setVisibilityListener(this);
        }
        controllerView.show();
        if(TextUtils.equals(pageListPlay.playUrl, videoUrl)){
            onPlayerStateChanged(true,Player.STATE_READY);
        }else {
            MediaSource mediaSource = PagedListPlayerManager.createMediaSource(videoUrl);
            exoPlayer.prepare(mediaSource);
            exoPlayer.setRepeatMode(Player.REPEAT_MODE_ONE);//无限播放
            pageListPlay.playUrl = videoUrl;
        }
        exoPlayer.addListener(this);
        exoPlayer.setPlayWhenReady(true);

    }

    @Override
    public void inActive() {
        PageListPlay pageListPlay = PagedListPlayerManager.get(category);
        SimpleExoPlayer exoPlayer = pageListPlay.exoPlayer;
        exoPlayer.setPlayWhenReady(false);
        playbtn.setVisibility(VISIBLE);
        playbtn.setImageResource(R.drawable.exo_icon_play);

    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        isPlaying=false;
        bufferView.setVisibility(VISIBLE);
        playbtn.setVisibility(VISIBLE);
        playbtn.setImageResource(R.drawable.exo_icon_play);

    }
    public View getPlayController() {
        PageListPlay listPlay = PagedListPlayerManager.get(category);
        return listPlay.controllerView;
    }
    @Override
    public boolean isPlay() {
        return isPlaying;
    }

    @Override
    public void onVisibilityChange(int visibility) {
        playbtn.setVisibility(visibility);
        playbtn.setImageResource(isPlaying?R.drawable.exo_icon_pause:R.drawable.exo_icon_play);

    }

    @Override
    public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
        PageListPlay pageListPlay = PagedListPlayerManager.get(category);
        SimpleExoPlayer exoPlayer = pageListPlay.exoPlayer;

        if(playbackState==Player.STATE_READY&&exoPlayer.getBufferedPosition()!=0){
            cover.setVisibility(INVISIBLE);
            bufferView.setVisibility(INVISIBLE);

        }else if(playbackState==Player.STATE_BUFFERING){
            bufferView.setVisibility(VISIBLE);
        }
        isPlaying=playbackState==Player.STATE_READY&&exoPlayer.getBufferedPosition()!=0&&playWhenReady;
        playbtn.setImageResource(isPlaying?R.drawable.exo_icon_pause:R.drawable.exo_icon_play);
    }
}
